Component({
  properties: {
    mode: Number,
    goodsType: Number,
    bizLine: Number,
    couponPrice: {
      type: Number,
      value: 0
    },
    jointCount: {
      type: Number,
      value: 0
    },
    userId: String,
    teacherId: String,
    productId: String,
    orderId: String,
    refundId: String,
    orderStatus: {
      type: Number,
      value: 0
    },
    marketingReady: {
      type: Boolean,
      value: false
    },
    refundTotalAmount: {
      type: Number,
      value: 0
    },
    goodsId: {
      type: String,
      value: "",
    },
  },
  data: {},

  methods: {
    getGoodsInfo(goodsId) {
      return new Promise((resolve) => {
        console.log("user goodsId", goodsId, this.properties.marketingReady);
        console.log("user properties", this.properties);
        resolve({
          // currentPrice: 1,
          // minLimits: 2,
          // maxLimits: 10,
          // dateRule: "",
          // goodsName: "循礼门M+丨【释集烤肉】99元  原价206.4元超值套餐",
          // goodsPhoto:
          //   "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.ibaotu.com%2Fgif%2F19%2F48%2F47%2F76Z888piCd6W.gif%21fwpaa50%2Ffw%2F700&refer=http%3A%2F%2Fpic.ibaotu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1644654365&t=5fc9b5fdad0a16264a9a9c09c14b3af9",
          // goodsLabels: [
          //   { type: "REFUND_ANYTIME" },
          //   { type: "BOOK_IN_ADVANCE", value: 98 },
          // ],
          // extra: { test: 123 },
          minLimits: 1,
          maxLimits: 2,
          dateRule: '周一至周日可用',
          validation: {
            phoneNumber: {
              required: false  // 手机号是否必填
            }
          },
          extra: {
            userId: this.properties.userId,
            teacherId: this.properties.teacherId,
            productId: this.properties.productId,
            coupon: this.properties.couponPrice,
            jointCount: this.properties.jointCount,
          }
        });
      });
    },

    applyrefund(e) {
      const {orderId} = e.detail;
      const extra = {}; // 开发者需要透传的参数，可自定义内容
      return new Promise(resolve => {
        resolve(extra);
      });
    },
    refund(e) {
      const {status, result} = e.detail;
      console.log(status, status)
      if (status === 'success') {
        const {refundId, outRefundNo} = result;
      } else {
        const {errMsg} = result;
      }
    },

    onError(e) {
      console.log(e.detail);
      const {errNo, errMsg} = e.detail;
      console.log("errNo", errNo, "errMsg", errMsg);
      if (errNo === 21514) {
        tt.showToast({
          title: "失败", // 内容
          icon: "none", // 图标
        });
      } else if (errNo === 21513) {
        tt.showToast({
          title: "获取中", // 内容
          icon: "none", // 图标
        });
      }
    },

    onPay(options) {
      const {status, orderId, outOrderNo, result} = options.detail;
      console.log("onPay", status, orderId, outOrderNo, result);
      if (status === 'success') {
        const {code} = result;
        if (code === 0) {
          // 支付成功
          this.triggerEvent('paySuccess', {orderId: outOrderNo})
        } else {
          this.triggerEvent('payFail', {orderId: outOrderNo, code})
          // 支付失败（超时、取消、关闭）
        }
      } else {
        // todo 订单超时时支付
        tt.navigateBack();
      }
    },

    handleRefund(event) {
      const {status, result} = event.detail;
      if (status === 'success') {
        const {refundId, outRefundNo} = result;
        console.log('发起退款成功', refundId, outRefundNo);
      } else {
        const {errMsg} = result;
        console.log('发起退款失败', errMsg);
      }
    },
    finishLogin(success) {
      console.log('finishLogin', success)
      if (success) {
        if (this._resolve) {
          this._resolve()
        }
      } else {
        if (this._reject) {
          this._reject()
        }
      }

      this._resolve = null
      this._reject = null
    },

    userLogin(event) {
      console.log("place order", event);
      this.triggerEvent('checkLogin')
      return new Promise((resolve, reject) => {
        this._resolve = resolve
        this._reject = reject
        // setTimeout(() => {
        //   resolve();
        // }, 0);
      });
    },

    userDyLogin(event) {
      console.log("place order", event);
      return new Promise((resolve, reject) => {
        tt.login({
          success: () => {
            resolve()
          },
          fail: () => {
            reject()
          }
        });
      })
    },
  },
});
